/**
 * 
 */
package com.jiang.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/**
 * @author Administrator
 *
 */
public class XMLDemoEle {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {

		File file = new File("Member.xml");
		InputStream is = new FileInputStream(file);
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db = dbf.newDocumentBuilder();
		Document document = db.parse(is);
		// 获取Member元素
		NodeList memberNodes = document.getElementsByTagName("member");
		for(int i=0; i<memberNodes.getLength();i++){
			Element memberElement = (Element)memberNodes.item(i);
			String idAttr = memberElement.getAttribute("id");
			System.out.print("元素Member的id为:" + idAttr+",");
			// 获取该Element的子元素
			Element nameEle = (Element)memberElement.getElementsByTagName("name").item(0);
			System.out.print(nameEle.getNodeName()+"="+nameEle.getTextContent()+",");
			Element phoneEle = (Element)memberElement.getElementsByTagName("phone").item(0);
			System.out.print(phoneEle.getNodeName()+"="+phoneEle.getTextContent()+",");
			Element addressEle = (Element)memberElement.getElementsByTagName("address").item(0);
			System.out.println(addressEle.getNodeName()+"="+addressEle.getTextContent());
			
		}
		is.close();
	}

}
